package net.nlimi.tel;

import net.nlimi.tel.mo.*;

import java.io.InputStream;
import java.util.Map;
import android.app.Activity;
import android.content.ContentUris;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class DetailsActivity extends Activity {
	private DetailsAdapter adapter;
	private ListView NTListView;

	 public void onCreate(Bundle savedInstanceState) 
	  { 
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.details);
	    this.getWindow().setBackgroundDrawableResource(R.drawable.white);
	    
	    Bundle bundle =this.getIntent().getExtras();
	    String contactId = bundle.getString("id");
	    
	    ImageView ico = (ImageView) findViewById(R.id.ico); 
	    TextView name = (TextView) findViewById(R.id.name);
	    
	    MoBContacts bc = new MoBContacts(getContentResolver());
	    Map<String, Object> prson = bc.getContactInfo(Long.parseLong(contactId));
	    name.setText((String)prson.get("name"));
	    
	    long photoId = (Long)prson.get("photoid");
	    Bitmap contactPhoto = null; Log.d("CD", ":"+photoId);
	    if (photoId>0){
	    	Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, Long.parseLong(contactId));  			
			InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(), contactUri);   
			contactPhoto = BitmapFactory.decodeStream(input);
			 Log.d("CD", ":"+contactUri);
			//contactPhoto = bc.getContactIco(photoId);
		}else {  
			contactPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.contact_photo);;
		}
	    ico.setImageBitmap(contactPhoto);
	    
	    adapter = new DetailsAdapter(this, contactId); 
	    NTListView = (ListView) findViewById(R.id.DetailsView);
        NTListView.setAdapter(adapter);

	  }
}
